package tms.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Booking{

    public static String tableName = "Booking";

    public static String[] columns = {"TicketNumber", "IDFlight", "IDBookingAccount",
            "PassengerName", "PassengerPassport", "PassengerPID", "TicketClass",
            "TotalAmount", "IsActive", "SeatNumber", "PassengerPhone", "PassengerEmail",
            "BookingPersonPhone", "BookingPersonEmail", "BookingPersonName",
            "BookingDay", "BookingMonth", "BookingYear", "BookingHour", "BookingMinute"};

    private int id = -1;
    private String ticketNumber = "";
    private int idFlight = -1;
    private int idBookingAccount = -1;
    private String passengerName = "";
    private String passengerPassport = "";
    private String passengerPID = "";
    private String ticketClass = "";
    private double totalAmount = 0;
    private boolean isActive = false;
    private int seatNumber = 0;
    private String passengerPhone = "";
    private String passengerEmail = "";
    private String bookingPersonPhone = "";
    private String bookingPersonEmail = "";
    private String bookingPersonName = "";

    private int bDay = 1;
    private int bMonth = 1;
    private int bYear = 2013;
    private int bHour = 0;
    private int bMin = 0;

    private Flight flight = null;
    private Account bookingAccount = null;

    static Booking createEntityFrom(Map<String, Object> map) {

        Booking entity = new Booking();

        entity.setId((Integer) map.get("Id".toLowerCase()));
        entity.setTicketNumber((String) map.get("TicketNumber".toLowerCase()));
        entity.setIdFlight((Integer) map.get("IdFlight".toLowerCase()));
        entity.setIdBookingAccount((Integer) map.get("IdBookingAccount".toLowerCase()));
        entity.setPassengerName((String) map.get("PassengerName".toLowerCase()));
        entity.setPassengerPassport((String) map.get("PassengerPassport".toLowerCase()));
        entity.setTicketClass((String) map.get("TicketClass".toLowerCase()));
        entity.setTotalAmount((Double) map.get("TotalAmount".toLowerCase()));
        entity.setActive((Integer) map.get("IsActive".toLowerCase()) == 1);
        entity.setSeatNumber((Integer) map.get("SeatNumber".toLowerCase()));
        entity.setPassengerPhone((String) map.get("PassengerPhone".toLowerCase()));
        entity.setPassengerEmail((String) map.get("PassengerEmail".toLowerCase()));
        entity.setBookingPersonPhone((String) map.get("BookingPersonPhone"));
        entity.setBookingPersonEmail((String) map.get("BookingPersonEmail"));
        entity.setBookingPersonName((String) map.get("BookingPersonName"));
        entity.setbDay((Integer) map.get("BookingDay".toLowerCase()));
        entity.setbMonth((Integer) map.get("BookingMonth".toLowerCase()));
        entity.setbYear((Integer) map.get("BookingYear".toLowerCase()));
        entity.setbHour((Integer) map.get("BookingHour".toLowerCase()));
        entity.setbMin((Integer) map.get("BookingMinute".toLowerCase()));

        return entity;
   }

    /**
     * Load Flight information by IDFlight.
     */
    private void bindingFlight(int idFlight) {
        if (idFlight > 0) {
            setFlight(Flight.createEntityFrom(DbManager.getFirstDynamic("Flight", "Flight.ID = " + idFlight)));
        }
    }

    /**
     * Load BookingAccount by IDBookingAccount.
     */
    private void bindingBookingAccount(int idBookingAccount) {
        if (idBookingAccount > 0) {
            setBookingAccount(Account.createEntityFrom(DbManager.getFirstDynamic("Account", "Account.ID = " + idBookingAccount)));
        }
   }

    public String[] getValues() {
        return new String[] {"'" + getTicketNumber() + "'", getIdFlight() + "", getIdBookingAccount() + "",
                "'" + getPassengerName() + "'", "'" + getPassengerPassport() + "'",
                "'" + getPassengerPID() + "'", "'" + getTicketClass() + "'", getTotalAmount() + "",
                isActive() == true ? "1" : "0", getSeatNumber() + "", "'" + getPassengerPhone() + "'",
                "'" + getPassengerEmail() + "'", "'" + getBookingPersonPhone() + "'", "'" + getBookingPersonEmail() + "'",
                "'" + getBookingPersonName() + "'", getbDay() + "", getbMonth() + "", getbYear() + "", getbHour() + "", getbMin() + ""};
   }

    /***** Following methods is important, please don't modify them *************/

    /**
     * Updating if obj is an existed object otherwise insert.
     * @param obj
     * @return true if success otherwise false
     */
    public static boolean save(Booking obj) {
        assert (obj != null);
        if (obj.getId() > 0) {
            return update(obj);
        } else {
            return insert(obj);
        }
   }

    /**
     * Insert given Booking to database.
     * @param obj
     * @return
     */
    private static boolean insert(Booking obj) {
        assert (obj instanceof Booking);
        return DbManager.insert(Booking.tableName, Booking.columns, ((Booking) obj).getValues());
   }

    private static boolean update(Booking obj) {
        assert (obj instanceof Booking);
        return DbManager.update(Booking.tableName, Booking.columns,
                ((Booking) obj).getValues(), "id = " + ((Booking) obj).getId());
   }

    public static boolean delete(Booking obj) {
        assert (obj instanceof Booking);
        return DbManager.delete(Booking.tableName,
                Booking.tableName + ".id = " + ((Booking) obj).getId());
   }

    /**
     * Get the corresponding Booking to where clause.
     * @param where condition (ex: Booking.IsActive = 1)
     * @return corresponding Booking or null
     */
    public static Booking getFirstDynamic(String where) {
        Map<String, Object> map = DbManager.getFirstDynamic(Booking.tableName, where);
        return (map != null ? (Booking) Booking.createEntityFrom(map) : null);
   }

    /**
     * Get a list of Booking corresponding to where clause.
     * @param where condition (ex: Booking.IsActive = 1)
     * @return corresponding list of Booking or an empty list
     */
    public static List<Booking> getListDynamic(String where) {
        List<Booking> lsResult = new ArrayList<Booking>();

        List<Map<String, Object>> rs = DbManager.getListDynamic(Booking.tableName, where);

        for (Map<String, Object> map : rs) {
            lsResult.add(Booking.createEntityFrom(map));
        }

        return lsResult;
   }

    /****************************************************************************/

    @XmlElement
    public String getTicketNumber() {
        return ticketNumber;
   }

    public void setTicketNumber(String ticketNumber) {
        this.ticketNumber = ticketNumber;
   }

    @XmlElement
    public int getIdFlight() {
        return idFlight;
   }

    public void setIdFlight(int idFlight) {
        this.idFlight = idFlight;
        bindingFlight(idFlight);
   }

    @XmlElement
    public int getIdBookingAccount() {
        return idBookingAccount;
   }

    public void setIdBookingAccount(int idBookingAccount) {
        this.idBookingAccount = idBookingAccount;
        bindingBookingAccount(idBookingAccount);
   }

    @XmlElement
    public String getPassengerName() {
        return passengerName;
   }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
   }

    @XmlElement
    public String getPassengerPassport() {
        return passengerPassport;
   }

    public void setPassengerPassport(String passengerPassport) {
        this.passengerPassport = passengerPassport;
   }

    @XmlElement
    public String getPassengerPID() {
        return passengerPID;
   }

    public void setPassengerPID(String passengerPID) {
        this.passengerPID = passengerPID;
   }

    @XmlElement
    public String getTicketClass() {
        return ticketClass;
   }

    public void setTicketClass(String ticketClass) {
        this.ticketClass = ticketClass;
   }

    @XmlElement
    public double getTotalAmount() {
        return totalAmount;
   }

    
    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
   }

    @XmlElement
    public boolean isActive() {
        return isActive;
   }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
   }

    @XmlElement
    public int getId() {
        return id;
   }

    private void setId(int id) {
        this.id = id;
   }

    public Flight getFlight() {
        return flight;
   }

    public void setFlight(Flight flight) {
        this.flight = flight;
        this.idFlight = flight.getId();
   }

    public Account getBookingAccount() {
        return bookingAccount;
   }

    public void setBookingAccount(Account bookingAccount) {
        this.bookingAccount = bookingAccount;
   }

    @XmlElement
    public int getSeatNumber() {
        return seatNumber;
   }

    public void setSeatNumber(int seatNumber) {
        this.seatNumber = seatNumber;
   }

    @XmlElement
    public String getPassengerPhone() {
        return passengerPhone;
   }

    public void setPassengerPhone(String passengerPhone) {
        this.passengerPhone = passengerPhone;
   }

    @XmlElement
    public String getPassengerEmail() {
        return passengerEmail;
   }

    public void setPassengerEmail(String passengerEmail) {
        this.passengerEmail = passengerEmail;
   }

    @XmlElement
    public String getBookingPersonPhone() {
        return bookingPersonPhone;
   }

    public void setBookingPersonPhone(String bookingPersonPhone) {
        this.bookingPersonPhone = bookingPersonPhone;
   }

    @XmlElement
    public String getBookingPersonEmail() {
        return bookingPersonEmail;
   }

    public void setBookingPersonEmail(String bookingPersonEmail) {
        this.bookingPersonEmail = bookingPersonEmail;
   }

    @XmlElement
    public String getBookingPersonName() {
        return bookingPersonName;
   }

    public void setBookingPersonName(String bookingPersonName) {
        this.bookingPersonName = bookingPersonName;
   }

    public Date getBookingDate() {
        Calendar c = Calendar.getInstance();
        c.set(getbYear(), getbMonth() - 1, getbDay(), getbHour(), getbMin());
        return c.getTime();
   }

    public void setBookingDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        setbDay(c.get(Calendar.DAY_OF_MONTH));
        setbMonth(c.get(Calendar.MONTH) + 1);
        setbYear(c.get(Calendar.YEAR));
        setbHour(c.get(Calendar.HOUR_OF_DAY));
        setbMin(c.get(Calendar.MINUTE));
   }

    @XmlElement
	public int getbDay() {
		return bDay;
	}

	private void setbDay(int bDay) {
		this.bDay = bDay;
	}

	@XmlElement
	public int getbMonth() {
		return bMonth;
	}

	public void setbMonth(int bMonth) {
		this.bMonth = bMonth;
	}

	@XmlElement
	private int getbYear() {
		return bYear;
	}

	private void setbYear(int bYear) {
		this.bYear = bYear;
	}

	@XmlElement
	private int getbHour() {
		return bHour;
	}

	private void setbHour(int bHour) {
		this.bHour = bHour;
	}

	@XmlElement
	private int getbMin() {
		return bMin;
	}

	private void setbMin(int bMin) {
		this.bMin = bMin;
	}
}
